package ua.kharkov.gritsay;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URLEncoder;

public class SendHTTPPOSTRequestWithSocket {

	public static void main(String[] args) {

		try {
			String params = URLEncoder.encode("name", "UTF-8") + "="
					+ URLEncoder.encode("post test", "UTF-8");
			params += "&" + URLEncoder.encode("email", "UTF-8") + "="
					+ URLEncoder.encode("null@null.nu", "UTF-8");
			params += "&" + URLEncoder.encode("enquiry", "UTF-8") + "="
					+ URLEncoder.encode("ssssssssssad adas das dasa das dasd asdasd asdasd as", "UTF-8");
			
			String hostname = "webfree.in.ua/klishe/index.php?route=information/contact";
			int port = 80;
			InetAddress addr = InetAddress.getByName(hostname);
			Socket socket = new Socket(addr, port);
			String path = "/index.php?route=information/contact";
			// Send headers
			BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(
					socket.getOutputStream(), "UTF8"));
			wr.write("POST " + path + " HTTP/1.0rn");
			wr.write("Content-Length: " + params.length() + "rn");
			wr.write("Content-Type: application/x-www-form-urlencodedrn");
			wr.write("rn");
			// Send parameters
			wr.write(params);
			wr.flush();
			// Get response
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
			String line;
			while ((line = rd.readLine()) != null) {
				System.out.println(line);
			}
			wr.close();
			rd.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
